type ComboBoxType = {
  Value: unknown;
  Name: string;
};
/**
 *
 */
export default class Common {
  static checkImg = (imgurl: string) => {
    var imgObj = new Image();
    imgObj.src = imgurl;
    //存在图片
    if (imgObj.width > 0 && imgObj.height > 0) {
      return true;
    } else {
      return false;
    }
  };
  static getFileName = (fileName: string) => {
    return fileName.replace(/(.*\/)*([^.]+).*/gi, '$2');
  };
  static getImage = (fileName: string) => {
    return '/icons/' + fileName;
    //return new URL(`//icons/${fileName}`, import.meta.url).href;
  };
  static setDefault = (dict: Array<ComboBoxType>) => {
    const obj = [{ Value: '', Name: '全部' } as ComboBoxType];
    return obj.concat(dict);
  };
  /**
   * 格式化时间
   * 调用formatDate(strDate, 'yyyy-MM-dd');
   * @param strDate（中国标准时间、时间戳等）
   * @param strFormat（返回格式）
   */
  static formatDate = (strDate: any, strFormat?: any) => {
    if (!strDate) {
      return;
    }
    if (!strFormat) {
      strFormat = 'yyyy-MM-dd';
    }
    var date = new Date(strDate); // 兼容safari
    var o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      S: date.getMilliseconds(),
    };
    if (/(y+)/.test(strFormat)) {
      strFormat = strFormat.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(strFormat)) {
        strFormat = strFormat.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
      }
    }
    return strFormat;
  };

  // 仿微信时间显示格式转换 @time 时间戳毫秒
  static weChatTimeFormat = time => {
    if (time == null) return '';
    const today = new Date().getTime();
    const timeDate = new Date(time);
    // 当前时间减去获取到的时间
    const sub = today - timeDate.getTime();
    const day = 1000 * 60 * 60 * 24;

    const currentYear = new Date().getFullYear();
    const getYear = new Date(time).getFullYear();
    const HHmm = `${this.formatDate(timeDate, 'hh')}:${this.formatDate(timeDate, 'mm')}`;
    const showWeekDay = isShowWeekDay(sub, timeDate.getDay());
    if (currentYear > getYear) {
      return `${this.formatDate(timeDate, 'yyyy-MM-dd')} ${this.formatDate(timeDate, 'hh:mm')}`;
    } else if (showWeekDay) {
      return `${weekFormat(timeDate.getDay())} ${HHmm}`;
    } else if (sub > day && sub < day * 2) {
      return `昨天 ${HHmm}`;
    } else if (sub <= day) {
      return HHmm;
    } else {
      return `${this.formatDate(timeDate, 'MM-dd')} ${this.formatDate(timeDate, 'hh:mm')}`;
    }
  };
}
function weekFormat(e, prefix = '周') {
  switch (e) {
    case 0:
      return prefix + '日';
      break;
    case 1:
      return prefix + '一';
      break;
    case 2:
      return prefix + '二';
      break;
    case 3:
      return prefix + '三';
      break;
    case 4:
      return prefix + '四';
      break;
    case 5:
      return prefix + '五';
      break;
    case 6:
      return prefix + '六';
      break;
    default:
      return '';
      break;
  }
}
// 转换今日的时辰格式
function todayTimeFormat(e) {
  if (e >= 0 && e < 7) {
    return '凌晨';
  } else if (e >= 7 && e < 11) {
    return '上午';
  } else if (e >= 11 && e < 13) {
    return '中午';
  } else if (e >= 13 && e < 18) {
    return '下午';
  } else if (e >= 18 && e < 24) {
    return '晚上';
  } else {
    return '';
  }
}
// 是否显示周几
function isShowWeekDay(sub, weekDay) {
  const currentWeekDay = new Date().getDay();
  const dayTime = 1000 * 60 * 60 * 24;
  // 1.当前时间与消息时间相差必须大于2天小于7天
  // 2.当前时间距离本周一相差必须大于2天且小于当前距离周一的天数
  // 3.消息时间不可能是0-周日，因为周日没有给后面时间留空间，不会走这里的逻辑，而是走今天的逻辑
  if (sub >= dayTime * 2 && weekDay !== 0 && sub <= dayTime * currentWeekDay) {
    return true;
  } else {
    return false;
  }
}
